home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap10 / HexDump / HexDoc.cpp next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  1.6 KB  |  72 lines

  1. //***********************************************************************
  2. //
  3. //  HexDoc.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "HexDoc.h"
  9.  
  10. IMPLEMENT_DYNCREATE (CHexDoc, CDocument)
  11.  
  12. CHexDoc::CHexDoc ()
  13. {
  14.     m_nDocLength = 0;
  15.     m_pFileData = NULL;
  16. }
  17.  
  18. void CHexDoc::DeleteContents ()
  19. {
  20.     if (m_pFileData != NULL) {
  21.         delete[] m_pFileData;
  22.         m_pFileData = NULL;
  23.         m_nDocLength = 0;
  24.     }
  25. }
  26.  
  27. void CHexDoc::Serialize (CArchive& ar)
  28. {
  29.     if (ar.IsLoading ()) {
  30.         CFile* pFile = ar.GetFile ();
  31.         m_nDocLength = (UINT) pFile->GetLength ();
  32.  
  33.         // Allocate a buffer for the file data
  34.         try {
  35.             m_pFileData = new BYTE[m_nDocLength];
  36.         }
  37.         catch (CMemoryException* e) {
  38.             m_nDocLength = 0;
  39.             throw e;
  40.         }
  41.  
  42.         // Read the file data into the buffer
  43.         try {
  44.             pFile->Read (m_pFileData, m_nDocLength);
  45.         }
  46.         catch (CFileException* e) {
  47.             delete[] m_pFileData;
  48.             m_pFileData = NULL;
  49.             m_nDocLength = 0;
  50.             throw e;
  51.         }
  52.     }
  53. }
  54.  
  55. UINT CHexDoc::GetBytes (UINT nIndex, UINT nCount, PVOID pBuffer)
  56. {
  57.     if (nIndex >= m_nDocLength)
  58.         return 0;
  59.  
  60.     UINT nLength = nCount;
  61.     if ((nIndex + nCount) > m_nDocLength)
  62.         nLength = m_nDocLength - nIndex;
  63.  
  64.     ::CopyMemory (pBuffer, m_pFileData + nIndex, nLength);
  65.     return nLength;
  66. }
  67.  
  68. UINT CHexDoc::GetDocumentLength ()
  69. {
  70.     return m_nDocLength;
  71. }
  72.